/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atm.controller;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.atm.domain.Client;
import org.atm.service.ClientService;

/**
 *
 * @author cj
 */
@ManagedBean(name="clientBean")
public class ClientBean {
    
    private Client selectedClient = new Client();
    
    @EJB
    private ClientService clientService;

    /** Creates a new instance of ClientBean */
    public ClientBean() {
    }

    public List<Client> getClients() {
        return clientService.getAll();
    }

    public List<Client> getNewClients() {
        return clientService.getAllNew();
    }
    
    public String approveClient() throws Exception {
        clientService.approveClient(selectedClient);
        addStatusMessage("Client " + selectedClient + " was approved");
        
        return "listNewClients";
    }

    public void rejectClient() {
        clientService.rejectClient(selectedClient);
        addStatusMessage("Client " + selectedClient + " was rejected");
    }

    public String viewClient() {
        return "viewClient";
    }

    public String add() {
        return "addClient";
    }

    public String save() {
        clientService.persist(selectedClient);
        return "registrationConfirmation?faces-redirect=true";
    }

    public String cancel() {
        return "welcome";
    }

    public String listNewClients() {
        return "listNewClients";
    }

    public Client getSelectedClient() {
        return selectedClient;
    }

    public void setSelectedClient(Client selectedClient) {
        this.selectedClient = selectedClient;
    }

    private void addStatusMessage(String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
    }

}
